﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TpOffice.Forms;
using TpOffice.Interfaces;
using TecBaseObjects.Models;
using TecData.Manager;
using TecBaseObjects.Interfaces;


namespace TpOffice.Controller {

    public class CashSaleController : IController {

        #region Members

        CashSaleForm myForm;

        #endregion

        #region Constructor

        public CashSaleController(ITecForm form) {
            this.myForm = form as CashSaleForm;
        }

        #endregion

        #region Methods

        public void CloseForm(object sender, EventArgs e) {
            myForm.Close();
        }

        public void CreateNewCashSale(object sender, EventArgs e) {
            
            CashSaleDetailForm myDetail = new CashSaleDetailForm(-1);
            myDetail.MyModel.InvoiceNo = -1;
            myDetail.ShowDialog();
            this.myForm.MyModel = CashSaleDataManager.LoadCashSales(true);
            this.myForm.SetFormDataSources();

        }

        public void Dispose() {
        }

        public void LoadModels() {

            this.myForm.MyModel = CashSaleDataManager.LoadCashSales(true);

        }

        public void OpenCashSaleDetail(object sender, DataGridViewCellMouseEventArgs e) {
            bool found = false;
            CashSaleDetailForm myDetail = new CashSaleDetailForm(this.myForm.MyModel[e.RowIndex].CashSaleID);
            myDetail.ShowDialog();

            foreach (CashSaleModel csm in this.myForm.MyModel) {
                if (csm.CashSaleID == myDetail.MyModel.CashSaleID) {
                    found = true;
                    break;
                }
            }

            if (!found) {
                this.myForm.MyModel = CashSaleDataManager.LoadCashSales(true);
            }

            if (!myDetail.MyModel.IsDirty) {
                this.myForm.MyModel[e.RowIndex].SalePrice = myDetail.MyModel.SalePrice;
                this.myForm.MyModel[e.RowIndex].Taxes = myDetail.MyModel.Taxes;
                this.myForm.MyModel[e.RowIndex].OrgKey = myDetail.MyModel.OrgKey;
                this.myForm.MyModel[e.RowIndex].OrgName = myDetail.MyModel.OrgName;
                this.myForm.MyModel[e.RowIndex].GrossPrice = myDetail.MyModel.GrossPrice;
                this.myForm.MyModel[e.RowIndex].InvoiceNo = myDetail.MyModel.InvoiceNo;
                this.myForm.MyModel[e.RowIndex].OrgFullName = myDetail.MyModel.OrgFullName;
            }
        }

        public void ReloadData(object sender, EventArgs e) {
            this.myForm.MyModel = CashSaleDataManager.LoadCashSales(true);
            this.myForm.SetFormDataSources();
        }

        public void Save(IBaseObject myObject) {
            throw new NotImplementedException("CashSaleController.Save");
        }

        public void ShowCoomingSoon() {
            MessageBox.Show("Comming soon...", "Barverkauf", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        #endregion

        #region Functions
        #endregion

    }
}
